package cn.iocoder.yudao.module.fm.service.fileinfourl;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fm.controller.admin.fileinfourl.vo.*;
import cn.iocoder.yudao.module.fm.dal.dataobject.fileinfourl.FileInfoUrlDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fm.convert.fileinfourl.FileInfoUrlConvert;
import cn.iocoder.yudao.module.fm.dal.mysql.fileinfourl.FileInfoUrlMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.fm.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 文件信息 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class FileInfoUrlServiceImpl implements FileInfoUrlService {

    @Resource
    private FileInfoUrlMapper fileInfoUrlMapper;

    @Override
    public Long createFileInfoUrl(FileInfoUrlCreateReqVO createReqVO) {
        // 插入
        FileInfoUrlDO fileInfoUrl = FileInfoUrlConvert.INSTANCE.convert(createReqVO);
        fileInfoUrlMapper.insert(fileInfoUrl);
        // 返回
        return fileInfoUrl.getId();
    }

    @Override
    public void updateFileInfoUrl(FileInfoUrlUpdateReqVO updateReqVO) {
        // 校验存在
        validateFileInfoUrlExists(updateReqVO.getId());
        // 更新
        FileInfoUrlDO updateObj = FileInfoUrlConvert.INSTANCE.convert(updateReqVO);
        fileInfoUrlMapper.updateById(updateObj);
    }

    @Override
    public void deleteFileInfoUrl(Long id) {
        // 校验存在
        validateFileInfoUrlExists(id);
        // 删除
        fileInfoUrlMapper.deleteById(id);
    }

    private void validateFileInfoUrlExists(Long id) {
        if (fileInfoUrlMapper.selectById(id) == null) {
            throw exception(DATA_NOT_EXISTS);
        }
    }

    @Override
    public FileInfoUrlDO getFileInfoUrl(Long id) {
        return fileInfoUrlMapper.selectById(id);
    }

    @Override
    public List<FileInfoUrlDO> getFileInfoUrlList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return fileInfoUrlMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FileInfoUrlDO> getFileInfoUrlPage(FileInfoUrlPageReqVO pageReqVO) {
        return fileInfoUrlMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FileInfoUrlDO> getFileInfoUrlList(FileInfoUrlExportReqVO exportReqVO) {
        return fileInfoUrlMapper.selectList(exportReqVO);
    }

}
